package com.daoos.swagger;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import lombok.extern.log4j.Log4j2;

/**
 * <p>
 * 所有Swagger2Markup类的父类
 * </p>
 */
@Log4j2
public abstract class AbstractSwagger2Markup {
	private Logger log = LogManager.getLogger(AbstractSwagger2Markup.class);

	/**
	 * 生成所的类的片段
	 * 
	 * @param mockMvc
	 *            MockMvc
	 * @throws Exception
	 */
	public void createSnippets(MockMvc mockMvc) throws Exception {
		ScanSnippet.snippetMap.forEach((K, V) -> {
			log.info("k={" + K + "},v{" + V + "}");
			String httpMethod = V.getHttpMethod();
			if (httpMethod != null) {
				MockHttpServletRequestBuilder requestBuilder = null;
				if (httpMethod.equalsIgnoreCase("get")) {
					requestBuilder = get(V.getURL());
				} else if (httpMethod.equalsIgnoreCase("post")) {
					requestBuilder = post(V.getURL());
				}
				assert requestBuilder != null;
				try {
					log.info("开始生成" + K + "的片段");
					if (V.getMediaType().equals(MediaType.APPLICATION_JSON)) {
						ResultActions resultActions = mockMvc
								.perform(requestBuilder.content(V.getContent()).params(V.getParams())
										.headers(V.getHeaders()).cookie(V.getCookie())
										.contentType(MediaType.APPLICATION_JSON))
								.andDo(document(K, preprocessResponse(prettyPrint())));
//						 resultActions.andExpect(status().isOk());
					} else if (V.getMediaType().equals(MediaType.APPLICATION_FORM_URLENCODED)) {
						ResultActions resultActions = mockMvc
								.perform(requestBuilder.params(V.getParams()).headers(V.getHeaders())
										.cookie(V.getCookie()).contentType(MediaType.APPLICATION_FORM_URLENCODED))
								.andDo(document(K, preprocessResponse(prettyPrint())));
						// resultActions.andExpect(status().isOk());
					}
					log.info("生成" + K + "的片段成功");
				} catch (Exception e) {
					log.error("生成" + K + "的片段失败：{}", e);
				}

			}
		});
	}
     @Value("${swagger.snippetOutput.dir}")
	public String snippetsOutputDir ;// 片断目录
	@Value("${swagger.input}")
	public String outputDir ;// swagger.json目录
	@Value("${generated.asciidoc.directory}")
	public String generatedOutputDir ;// asciiDoc目录
//
//	public String snippetsOutputDir = System.getProperty("swagger.snippetOutput.dir");// 片断目录
//	public String outputDir = System.getProperty("swagger.input");// swagger.json目录
//	public String generatedOutputDir = System.getProperty("generated.asciidoc.directory");// asciiDoc目录

//
//	public String snippetsOutputDir = System.getProperty("io.springfox.staticdocs.snippetsOutputDir");// 片断目录
//	public String outputDir = System.getProperty("io.springfox.staticdocs.outputDir");// swagger.json目录
//	public String generatedOutputDir = System.getProperty("io.springfox.staticdocs.generatedOutputDir");// asciiDoc目录
//

}